package com.heu.blood.security.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.R;
import com.heu.blood.security.dto.req.SpecialLoginReqDTO;
import com.heu.blood.security.dto.resp.SpecialLoginRespDTO;
import com.heu.blood.security.entity.LoginUser;
import com.heu.blood.security.entity.UserEntity;
import com.heu.blood.security.service.LoginService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ControllerLogAnnotation(module = "登录模块", operate = "用户登录")
    public R login(@RequestBody UserEntity user, HttpServletResponse response){
        Map<String, Object> map = loginService.login(user);
        return R.ok("登录成功").put("map", map);
    }

    /**
     * 用户登出
     * @return
     */
    @PostMapping("/logout")
    @ControllerLogAnnotation(module = "登录模块", operate = "用户登出")
    public R logout(){
        loginService.logout();
        return R.ok("退出成功");
    }

    /**
     * 用户注册
     * @return
     */
    @PostMapping("/register")
    @ControllerLogAnnotation(module = "登录模块", operate = "用户注册")
    public R register(UserEntity userEntity){
       Pair<Boolean, String> res = loginService.register(userEntity);
       if (!res.getLeft()){
           return R.ok(res.getRight());
       }
       return R.ok(res.getRight());
    }

    /**
     * 进行特殊配血和特殊发血的的二次登录
     * @param requestParam
     * @return
     */
    @PostMapping("/specialLogin")
    @ControllerLogAnnotation(module = "登录模块", operate = "进行特殊配血和特殊发血的的二次登录")
    public R specialLogin(@RequestBody SpecialLoginReqDTO requestParam){
        SpecialLoginRespDTO specialLoginRespDTO = loginService.specialLogin(requestParam);
        return R.ok().put("list", specialLoginRespDTO);
    }

    /**
     * 获取登录用户信息
     */
    @GetMapping("/testLogin")
    @ControllerLogAnnotation(module = "登录模块", operate = "获取登录用户信息")
    public void getLoginUser(){
        // 获取当前线程的认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 检查认证信息是否存在并判断用户是否已经通过身份验证
        if (authentication != null && authentication.isAuthenticated()){
            // 获取登录用户信息
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            // 获取当前登录用户id
            Long userId = loginUser.getUser().getId();
            // 获取当前登录用户名
            String userName = loginUser.getUser().getUserName();
            System.out.println("userId:" + userId + " "  + "userName:" + userName);
        }
    }
}